package com.shty.wd.utils.module.Britain;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import com.shty.wd.utils.module.IOTools.IOUtil;
import com.shty.wd.utils.module.IOTools.PropertyUtil;
import com.shty.wd.utils.module.related.Entity;
import com.shty.wd.utils.module.related.JsonParse;

import net.sf.json.JSONObject;
import net.sf.json.util.JSONStringer;

/**
 * @description Realte_UK output: dic/result.txt. output form: jsonArray.
 *              console output: single result correspond to a json file. output
 *              form: json.
 */
public class test {

	public static void main(String[] args) throws Exception {
		if (RelationAnalysis.DIC_LOCATION.length() == 0) {
			RelationAnalysis.loadLocation();
		}
		if (RelationAnalysis.DIC_NAME.length() == 0) {
			// RelationAnalysis.loadNameDic();
		}
		if (RelationAnalysis.DIC_ORG.length() == 0) {
			RelationAnalysis.loadORG();
		}

		
		JsonParse jp = new JsonParse();

		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				PropertyUtil.getValues("Britain.output")), "UTF-8");

		List<String> pathlist = new ArrayList<>();
		pathlist.add("Britain.iutput.facebook");
		pathlist.add("Britain.iutput.linkin");
		for (String path : pathlist) {
			File dir = new File(PropertyUtil.getValues(path));
			for (File file : dir.listFiles()) {
				String data = IOUtil.readDoc(file);
				JSONObject object = JSONObject.fromObject(data);
				Entity entity = jp.getEntity(object);
				RelationAnalysis.CheckRelation(entity);

				JSONStringer js = new JSONStringer();

				String idKey = null;
				String id = null;
				String url = null;
				String urlKey = null;
				if (entity.getResult().containsKey("fbid")) {
					idKey = "fbid";
					urlKey = "fburl";

				} else {
					idKey = "lnkid";
					urlKey = "lnkurl";
				}
				id = entity.getResult().get(idKey);
				url = entity.getResult().get(urlKey);

				String name = entity.getResult().get("fmt_name");
				String headline = entity.getResult().get("fmt_headline");
				boolean bRelation = entity.getHasRelations();
				String basis = entity.getBasis();

				if (entity.getResult().containsKey("fbid")) {
					js.object().key(idKey).value(id).key("fmt_headline")
							.value(headline).key("fmt_name").value(name)
							.key("hasRelations").value(bRelation).key("basis")
							.value(basis).endObject();
				} else {
					String location = entity.getResult().get("fmt_location");
					js.object().key(idKey).value(id).key("fmt_headline")
							.value(headline).key("fmt_name").value(name)
							.key("fmt_location").value(location)
							.key("hasRelations").value(bRelation).key("basis")
							.value(basis).endObject();
				}
				osw.write(js.toString() + "\n");
			}
		}
		osw.close();
	}
}
